package element
{
    import control.KeyboardCtrl;
    
    import data.Map;
    
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.TimerEvent;
    import flash.utils.Timer;
    
    /**
     *游戏主角显示对象  
     * @author Felix
     * 
     */    
    public class Person extends Sprite
    {
        private var _role:MovieClip;
        private var _ctrl:KeyboardCtrl;
        private var _width:uint;
        private var _height:uint;
		/**
		 *当前速度 
		 */		
        private var _speed:uint = 4;
		/**
		 *最大速度 
		 */		
		private var _maxSpeed:uint = 10;
		/**
		 *最小速度 
		 */		
		private var _minSpeed:uint = 1;
		/**
		 *当前泡泡数 
		 */		
		private var _bubbles:uint = 0;
		/**
		 *可用泡泡数 
		 */		
		private var _putBubbles:uint = 0;
		/**
		 *锁定状态 
		 */		
		private var _lock:Boolean = false;
        private var _frame:String = null;
        
        public var _moveElem:Function = null;
        public var _addPaopao:Function = null;
		

        public function Person(role:MovieClip, 
                               ctrl:KeyboardCtrl)
        {
            _role = role;
            _ctrl = ctrl;
            //_width = role.hitRect.width;
            //_height = role.hitRect.height;
            _width = 32;
            _height = 32;

            addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        }
        
        private function onAddedToStage(event:Event):void
        {            
            addChild(_role);
            _role.gotoAndStop("birth");
            _frame = "birth";

            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }

        private function doArtificialIntelligence():void
        {
            trace("I'm controled by artificial intelligence!");
        }

		/**
		 * 根据键盘的按键方向的布尔值，调用相应的移动函数
		 * @param event
		 * 
		 */		
        protected function onEnterFrame(event:Event):void
        {
            if (_ctrl == null) {
                doArtificialIntelligence();
                return;
            }

            if (_ctrl.BUBBLE) {
                addPaopao();
            } else if (_ctrl.UP) {
                moveUp();
            } else if (_ctrl.DOWN) {
                moveDown();
            } else if (_ctrl.LEFT) {
                moveLeft();
            } else if (_ctrl.RIGHT) {
                moveRight();
            } else {
                switch (_frame) {
                    case "left":
                        gotoFrame("leftstop");
                        break;
                    case "right":
                        gotoFrame("rightstop");
                        break;
                    case "up":
                        gotoFrame("upstop");
                        break;
                    case "down":
                        gotoFrame("downstop");
                        break;
                }
            }

            if (Map.data[this.row][this.col] == Map.E_GrassHinder) {
                _role.alpha = 0.4;
            } else {
                _role.alpha = 1;
            }
        }
        
        public function gotoFrame(newFrame:String):void
        {
            if (_frame != newFrame) {
                _frame = newFrame;
                _role.gotoAndStop(newFrame);
            }
        }
		/**
		 *按下右键向右移动 
		 * 
		 */        
        private function moveRight():void
        {
            gotoFrame("right");
            
            if(checkFinder("right", this.x + _width + _speed, this.y)) return;
            if(checkFinder("right", this.x + _width + _speed, this.y + _height)) return;
            
            this.x += _speed;
        }
		/**
		 *按下左键向左移动 
		 * 
		 */        
        private function moveLeft():void
        {
            gotoFrame("left");
            
            if(checkFinder("left", this.x - _speed, this.y)) return;
            if(checkFinder("left", this.x - _speed, this.y + _height)) return;
            
            this.x -= _speed;
        }
		/**
		 *按下下键向下移动 
		 * 
		 */        
        private function moveDown():void
        {
            gotoFrame("down");

            if(checkFinder("down", this.x, this.y + _height + _speed)) return;
            if(checkFinder("down", this.x + _width, this.y + _height + _speed)) return;
            
            this.y += _speed;
        }
		/**
		 *按下上键向上移动 
		 * 
		 */        
        private function moveUp():void
        {
            gotoFrame("up");

            if(checkFinder("up", this.x, this.y - _speed)) return;
            if(checkFinder("up", this.x + _width, this.y - _speed)) return;

            this.y -= _speed;
        }
		/**
		 *按下空格键添加泡泡 
		 * 
		 */        
        private function addPaopao():void
        {
            if (_addPaopao != null) {
				if (Map.data[this.row][this.col] == Map.E_Floor) {
                	_addPaopao(this);
				}
            }
        }
        
        public function get row():int
        {
            return int((this.y + _height / 2) / Map.GAP);
        }
        
        public function get col():int
        {    
            return int((this.x + _width /2) / Map.GAP);
        }
        
		/**
		 * 检查是否被阻挡
		 * @param dir
		 * @param x
		 * @param y
		 * @return 
		 * 
		 */		
        private function checkFinder(dir:String, x:int, y:int):Boolean
        {
            var r:int;
            var c:int;
            var nr:int;
            var nc:int;
            
            if (x < 0 || x > Map.col * Map.GAP)
                return true;

            if (y < 0 || y > Map.row * Map.GAP)
                return true;

            r = int(y / Map.GAP);
            c = int(x / Map.GAP);

            if (c >= Map.col)
                return true;
            if (r >= Map.row)
                return true;

            if (Map.data[r][c] == undefined) 
                return true;

            if (Map.data[r][c] == Map.E_Paopao)
                return false;

            if (Map.data[r][c] == Map.E_Floor)
                return false;

            if (Map.data[r][c] == Map.E_GrassHinder)
                return false;

            if (Map.data[r][c] == Map.E_BoxHinder) {
                nr = r; nc = c;
                switch (dir) {
                    case "left": 
                        nc--;
                        //gotoFrame(leftpush);
                        break;
                    case "right": 
                        nc++;
                        //gotoFrame(rightpush);
                        break;
                    case "down": 
                        nr++;
                        //gotoFrame(downpush);
                        break;
                    case "up": 
                        nr--;
                        //gotoFrame(uppush);
                        break;
                }

                if (nr < 0 || nc < 0)
                    return true;
            
                if (nr > Map.row || nc > Map.col)
                    return true;

                if (Map.data[nr][nc] == Map.E_Floor) {
                    if (_moveElem != null) {
                        _moveElem(r, c, Map.E_BoxHinder, nr, nc, Map.E_Floor);
                        return false;
                    }
                }

                return true;
            }

            return true;
        }
    }
}
